package com.jiazhong.mingxing.boot.boot10.controller;

import com.jiazhong.mingxing.boot.boot10.bean.Book;
import io.swagger.v3.oas.annotations.ExternalDocumentation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/book")
@Tag(name = "bookController", description = "图书接口，实现了图书的添加、修改、上传图片、获取图书信息等操作",
        externalDocs = @ExternalDocumentation(description = "参考⽂档", url =
                "https://www.baidu.com"))
public class BookController {
    @PostMapping("/save")
    @Operation(
            description = "图书录入接口，将数据录入到数据库中",
            parameters = {
                    @Parameter(name = "bookName", description = "图书名称", required = true),
                    @Parameter(name = "number", description = "图书的数量，要求大于0"),
                    @Parameter(name = "price", description = "图书的价格，单位人民币")
            },
            responses = {
                    @ApiResponse(
                            description = "成功的返回,返回success", responseCode = "200"

                    ),
                    @ApiResponse(
                            description = "失败的返回,返回error", responseCode = "500"
                    )
            }
    )
    public String save(String bookName, int number, double price, HttpServletResponse response) {
        int num = (int) (Math.random() * 100 + 1);
        if (num % 2 == 0) {
            response.setStatus(200);
            return "success";
        }
        response.setStatus(500);
        return "error";
    }

    @Operation(
            description = "修改API接口",
            parameters = {
                    @Parameter(name = "book")
            }
    )
    @PutMapping("/update")
    public String update(Book book) {
        return "success";
    }
}
